<template>
  <view class="main">
    <template v-if="factoryList?.length > 0">
      <CompanyCard @boxClick="boxClick(item)" v-for="(item, index) in factoryList" :key="index" :info="item" :isBtn="true" />
      <u-loadmore height="30" :status="status" />
    </template>
    <view v-else style="width: 100%; display: flex; justify-content: center">
      <u-empty mode="list"></u-empty>
    </view>
    <view class="buttomBox">
      <u-button color="#1676fc" shape="circle" @click="goAdd">添加工厂</u-button>
    </view>
  </view>
</template>

<script setup lang="ts">
import CompanyCard from "@/components/companyCard.vue";
import { goPage } from "@/utils/commonFn";
//@ts-ignore
import { onReachBottom, onShow } from "@dcloudio/uni-app";
import { getFactoryList } from "@/api/my";
import { onMounted, ref } from "vue";
const formData = ref({
  pageNum: 1,
  pageSize: 10,
});
onShow(() => {
  rest();
});
const status = ref("loadmore");
const factoryList = ref<HomeTypes.factoryList[]>([]);
const getList = async () => {
  status.value = "loadmore";
  const res: any = await getFactoryList(formData.value);
  factoryList.value = factoryList.value.concat(res.rows);
  if (factoryList.value.length >= (res.total as number)) {
    status.value = "nomore";
  } else {
    status.value = "loadmore";
  }
};
const rest = () => {
  formData.value.pageNum = 1;
  factoryList.value = [];
  status.value = "loadmore";
  getList();
};
onReachBottom(() => {
  formData.value.pageNum++;
  getList();
});
const boxClick = (info: HomeTypes.factoryList) => {
  goPage(`/pages/myPage/myFactory/factorySettlement?id=${info.id}`);
};
const goAdd = () => {
  const userInfo: myTypes.userInfo = uni.getStorageSync("userInfo");
  if (!userInfo.mobile) {
    uni.showModal({
      title: "提示",
      cancelText: "否",
      confirmText: "是",
      content: "您当前未登录，是否前往登录",
      success: function (res) {
        if (res.confirm) {
          goPage("/pages/myPage/login");
        }
      },
    });
    return;
  }
  if (userInfo.cardFlag == 0) {
    uni.showModal({
      title: "提示",
      cancelText: "否",
      confirmText: "是",
      content: "您当前还没有名片，是否前往创建",
      success: function (res) {
        if (res.confirm) {
          goPage("/pages/myPage/MyBusinessCard/createBusinessCard");
        }
      },
    });
    return;
  }
  if (userInfo.authFlag == "0") {
    uni.showModal({
      title: "提示",
      cancelText: "否",
      confirmText: "是",
      content: "您当前还未实名认证，是否前往",
      success: function (res) {
        if (res.confirm) {
          goPage("/pages/myPage/businessCardAuth");
        }
      },
    });
    return;
  }
  if (userInfo.level !== "1") {
    uni.showModal({
      title: "提示",
      cancelText: "否",
      confirmText: "是",
      content: "您未开通会员，是否前往",
      success: function (res) {
        if (res.confirm) {
          goPage("/pages/myPage/activateMembership");
        }
      },
    });
    return;
  }
  goPage(`/pages/myPage/myFactory/factorySettlement`);
};
</script>

<style lang="less" scoped></style>
